﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using padnugSlMVC.CalendarServiceReference;

namespace padnugSlMVC
{
    public partial class MainPage : Page
    {
        private List<DateTime> CalendarDates;
        public MainPage()
        {
            InitializeComponent();
            #region new code

            this.CalendarDates = new List<DateTime>();
            HtmlPage.RegisterScriptableObject("Page", this);
 
            #endregion
            this.calendar1.SelectionMode = CalendarSelectionMode.MultipleRange;
            
        }

        #region new code called from javascript

        [ScriptableMember]
        public void UpdateDates(string result)
        {
            var dateStrs = result.Split(',').ToList();
            foreach (var d in dateStrs)
            {
                var dt = Convert.ToDateTime(d);
                this.CalendarDates.Add(dt);
                this.calendar1.SelectedDates.Add(dt);
            }

            this.calendar1.SelectedDatesChanged += new EventHandler<SelectionChangedEventArgs>(calendar1_SelectedDatesChanged);
        }
 
        #endregion

        private void calendar1_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDate = ((Calendar)sender).SelectedDate.HasValue ? ((Calendar)sender).SelectedDate.Value : Convert.ToDateTime("01/01/1900");
            var isMeetingDate = this.CalendarDates.Any(d => d == selectedDate);
            var absUri = HtmlPage.Document.DocumentUri.AbsoluteUri.ToString();
            var documentUri = HtmlPage.Document.DocumentUri;

            var newDocPath = string.Empty;
            Uri newUri = null;
            if (documentUri.LocalPath.Equals("/"))
            {
                newDocPath = "Events/EventsOnDate?date=";
                newUri = new Uri(newDocPath + selectedDate.ToShortDateString(),UriKind.Relative);
            }
            else if (documentUri.LocalPath.Contains("EventsOnDate"))
            {
                newDocPath = documentUri.LocalPath + "?date=";
                newUri = new Uri(newDocPath + selectedDate.ToShortDateString(), UriKind.Relative);
            }
            else if (documentUri.LocalPath.EndsWith("/Events"))
            {
                newDocPath = "Events/EventsOnDate?date=";
                newUri = new Uri(newDocPath + selectedDate.ToShortDateString(), UriKind.Relative);
            }
            else
            {
                // code will get here if application root
                // is not "/" (as in http://localhost/padnug )
                newDocPath = documentUri.LocalPath + "/Events/EventsOnDate?date=";
                newUri = new Uri(newDocPath + selectedDate.ToShortDateString(), UriKind.Relative);
            }
            
            if (isMeetingDate)
            {
                HtmlPage.Window.Navigate(newUri);
            }
            else
            {
                var uri = HtmlPage.Document.DocumentUri;
                HtmlPage.Window.Navigate(uri);
            }
        }
    }
}
